﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;


using SmallProject.Models.CutstomeUI;
using Small.Util.Reflection;

namespace SmallProject.UserControls.Extension
{
    public static class HtmlSelectItems
    {
        /// <summary>
        /// 下拉列表
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="name">名称</param>
        /// <param name="source">资源</param>
        /// <param name="selectedValue">选中项</param>
        /// <param name="displayText">显示文本</param>
        /// <param name="htmlAttribute">Html属性</param>
        /// <param name="language">语言</param>
        /// <returns></returns>
        public static MvcHtmlString SelectList(this HtmlHelper helper, string name,
                              IList<SelectItem> source,object selectedValue,object htmlAttribute=null,string displayText="请选择")
        {
           
            
            StringBuilder builder = new StringBuilder();


            

            builder.Append("<select ");
            builder.Append(" name=\"" + name + "\"");

            if (htmlAttribute != null)
            {
                foreach (var item in AnonymousProperties.GetAnonymousInfomation(htmlAttribute))
                {
                    builder.Append(" " + item.Name + "=\"" + item.Value + "\"");

                }
            }
            builder.Append(">\r\n");
              
                  builder.Append(" <option>" + displayText + "</option>\r\n");
              
                  
              



              if (source != null)
              {
                  var selectedItem = source.Where(p => Convert.ToString(p.Value).Equals(Convert.ToString(selectedValue))).FirstOrDefault();

                  if (selectedItem != null)
                  {
                      selectedItem.IsDefault = true;


                  }

                  foreach (var item in source)
                  {

                      builder.Append("  <option  value=\"" + item.Value + "\"");


                      string text =item.Text;
 
                      if (item.IsDefault)
                      {
                          builder.Append("selected=\"selected\"");
                      }
                      builder.Append(">"+item.Text+"</option> \r\n");
                  }
              }

            builder.Append("</select>\r\n");
            return new MvcHtmlString(builder.ToString());
        }

        public static MvcHtmlString SelectList(this HtmlHelper helper, string name,
                                 IList<SelectItem> source) 
        {

           return SelectList(helper, name, source, null);
        }

        /// <summary>
        /// 下拉列表
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="name">名称</param>
        /// <param name="source">列表资源</param>
        /// <param name="selectedValue">选中项</param>
        /// <returns></returns>
        public static MvcHtmlString SelectList(this HtmlHelper helper, string name,
                              IList<SelectItem> source, object selectedValue) 
        {

            return SelectList(helper, name, source, selectedValue,null);
        }
        /// <summary>
        /// 下拉列表
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="name">名称</param>
        /// <param name="source">列表资源</param>
        /// <param name="selectedValue">选中项</param>
        /// <param name="htmlAttribute">Html属性</param>
        /// <returns></returns>
        public static MvcHtmlString SelectList(this HtmlHelper helper, string name,
                             IList<SelectItem> source, object selectedValue, string displayText, object htmlAttribute) 
        {
            return SelectList(helper, name, source, selectedValue, htmlAttribute,displayText);
        }


        

        
    }
}